package com.imufe.chemicalenterprisedangerwarning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.imufe.chemicalenterprisedangerwarning.entity.po.UserPO;
import com.imufe.chemicalenterprisedangerwarning.entity.query.LoginQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.RoleVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.SafetySupervisionBureauUserDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.SafetySupervisionBureauVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.UserDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.common.InfoCode;
import com.imufe.chemicalenterprisedangerwarning.exception.UserException;
import com.imufe.chemicalenterprisedangerwarning.mapper.UserMapper;
import com.imufe.chemicalenterprisedangerwarning.mapper.UserRoleMapper;
import com.imufe.chemicalenterprisedangerwarning.service.RoleService;
import com.imufe.chemicalenterprisedangerwarning.service.UserRoleService;
import com.imufe.chemicalenterprisedangerwarning.service.UserSafetySupervisionBureauService;
import com.imufe.chemicalenterprisedangerwarning.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imufe.chemicalenterprisedangerwarning.util.BeanUtils;
import com.imufe.chemicalenterprisedangerwarning.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author BaiBan
 * @since 2022-10-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserService {

    @Autowired
    UserMapper mapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    UserSafetySupervisionBureauService userSafetySupervisionBureauService;

    @Autowired
    RoleService roleService;

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    UserDetailsService userDetailsService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public String login(LoginQuery login) throws UserException {
        // 获取数据库的用户信息
        UserDetails user = userDetailsService.loadUserByUsername(login.getUsername());

        // 比较 数据库密码 和 输入的密码
        if (!passwordEncoder.matches(login.getPassword(), user.getPassword()))
            throw new UserException(InfoCode.USER_CREDENTIALS_ERROR);

        LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPO::getUsername, login.getUsername());
        UserPO userPO = mapper.selectOne(wrapper);
        // 查找用户id
        Long userId = userPO.getId();

        return JwtUtil.createJWT(String.valueOf(userId));
    }

    @Override
    public UserDetailedVO detailed(Long id) {
        UserPO po = mapper.selectById(id);
        return BeanUtils.copyPOJO(po, UserDetailedVO.class);
    }

    @Override
    public SafetySupervisionBureauUserDetailedVO safetySupervisionBureauUserDetailed(Long id) {
        // 用户基本信息
        UserDetailedVO userDetailedVO = this.detailed(id);
        SafetySupervisionBureauUserDetailedVO vo = BeanUtils.copyPOJO(userDetailedVO, SafetySupervisionBureauUserDetailedVO.class);

        // 安检局基本信息
        SafetySupervisionBureauVO safetySupervisionBureauVO = userSafetySupervisionBureauService.getSafetySupervisionBureau(id);
        vo.setSafetySupervisionBureau(safetySupervisionBureauVO);

        return vo;
    }

    @Override
    public List<String> getUserAuthorities(Long userId) {

//        RoleVO role = userRoleService.getRole(userId);
//        List<String> roleList = new LinkedList<>();
//        roleList.add(role.getName());
//        return roleList;
        return null;
    }

    @Override
    public String getUserOrganization(Long id) {

        // 获取角色id
        Long roleId = userRoleMapper.selectRoleIdByUserId(id);

        // if 是 2就是安监局检测员
        if (roleId == 2)
            return userSafetySupervisionBureauService.getSafetySupervisionBureau(id).getName();
//        if (roleId == 4)
//            return
        // TODO 企业用户写完以后要把这里补充了
        return null;
    }
}